package ch.ethz.fcl.metrobuzz.data.singapore;

import java.io.File;
import java.util.ArrayList;

import ch.ethz.fcl.metrobuzz.data.scene.MBScene;
import ch.ethz.fcl.metrobuzz.spatialindex.object2d.Polygon;
import ch.ethz.fcl.metrobuzz.tools.MBTools;
import diewald_shapeFile.files.dbf.DBF_File;
import diewald_shapeFile.files.shp.SHP_File;
import diewald_shapeFile.files.shp.shapeTypes.ShpPolygon;
import diewald_shapeFile.files.shx.SHX_File;
import diewald_shapeFile.shapeFile.ShapeFile;

public class SGZone {
	public static final int LEVELS = 1;
	private MBScene scene = null;

	@SuppressWarnings("unchecked")
	private ArrayList<Polygon>[] zones = new ArrayList[LEVELS];

	private ArrayList<Polygon> singapore;

	public SGZone(MBScene scene) {
		this.scene = scene;
		for (int i = 0; i < LEVELS; i++) {
			zones[i] = new ArrayList<Polygon>();
		}
		singapore = new ArrayList<Polygon>();
	}

	public void init() {
		loadZones("1regions", "subZoneUTM48N", singapore);
		if (zones.length > 0)
			loadZones("5regions", "subZoneUTM48N", zones[0]);
		if (zones.length > 1)
			loadZones("14regions", "subZoneUTM48N", zones[1]);
		if (zones.length > 2)
			loadZones("22regions", "subZoneUTM48N", zones[2]);
		if (zones.length > 3)
			loadZones("36regions", "subZoneUTM48N", zones[3]);
		if (zones.length > 4)
			loadZones("54regions", "subZoneUTM48N", zones[4]);
		if (zones.length > 5)
			loadZones("96regions", "subZoneUTM48N", zones[5]);
		if (zones.length > 6)
			loadZones("174regions", "subZoneUTM48N", zones[6]);
	}

	private void loadZones(String folderName, String fileName,
			ArrayList<Polygon> zones) {
		DBF_File.LOG_INFO = false;
		DBF_File.LOG_ONLOAD_HEADER = false;
		DBF_File.LOG_ONLOAD_CONTENT = false;

		SHX_File.LOG_INFO = false;
		SHX_File.LOG_ONLOAD_HEADER = false;
		SHX_File.LOG_ONLOAD_CONTENT = false;

		SHP_File.LOG_INFO = false;
		SHP_File.LOG_ONLOAD_HEADER = false;
		SHP_File.LOG_ONLOAD_CONTENT = false;

		ShapeFile shapefile;
		String dir = "res/Singapore/zones/";

		if (!new File(dir).exists())
			dir = dir.replace("res/", "");

		// LOAD SHAPE FILE (.shp, .shx, .dbf)
		try {
			shapefile = new ShapeFile(dir + folderName, fileName).READ();

			int number_of_shapes = shapefile.getSHP_shapeCount();
			MBTools.debug("parts " + shapefile.getSHP_shape().size(), false);
			MBTools.debug("parts dbf " + shapefile.getDBF_fieldCount(), false);

			for (int i = 0; i < number_of_shapes; i++) {
				ShpPolygon shape = shapefile.getSHP_shape(i);
				double[][][] parts;
				if (shape.getNumberOfParts() > 1) {
					parts = shape.getPointsAs3DArray();
					for (int j = 0; j < parts.length; j++)
						zones.add(readZone(parts[j]));
				} else {
					Polygon zone = readZone(shape.getPoints());
					if (zone != null)
						zones.add(zone);
				}
			}

			MBTools.debug("subzone size " + zones.size(), false);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private Polygon readZone(double[][] points) {
		Polygon zone = new Polygon();
		for (int j = 0; j < points.length; j++) {
			float x = scene.getBound().normalizeX((float) points[j][0]);
			float y = scene.getBound().normalizeY((float) points[j][1]);
			zone.addPoint(x, y);
		}

		return zone;
	}

	public ArrayList<Polygon> getSingaporeZone() {
		return singapore;
	}

	public int getZoneSize(int level) {
		return zones[level].size();
	}

	public Polygon getZone(int level, int i) {
		if (i >= getZoneSize(level))
			return null;
		if (level >= zones.length)
			return null;
		return zones[level].get(i);
	}

	public ArrayList<Polygon> getZones(int level) {
		if (level >= zones.length)
			return null;
		return zones[level];
	}
}
